package com.android.allwinner.newaw360.task;

import android.os.AsyncTask;

import com.android.allwinner.newaw360.common.APPConfig;
import com.android.allwinner.newaw360.utils.AW360SharePreferenceUtil;
import com.android.allwinner.newaw360.utils.FileOrderUtils;
import com.android.allwinner.newaw360.utils.FileUtils;
import com.android.allwinner.newaw360.utils.SDCardUtils;

import java.io.File;
import java.util.List;

/**
 * Created by xiasj on 17-7-6.
 * 处理循环录像时的文件删除
 */

public class DeleteFileTask extends AsyncTask {

    private List<String> mFileList;

    @Override
    protected void onPreExecute() {
        //查找文件排序
        sortFiles();
        super.onPreExecute();
    }

    private void sortFiles() {
        File path = new File(AW360SharePreferenceUtil.getVideoPath());
        if (!path.exists()) {
            return;
        }
        String[] files = path.list();
        mFileList = FileOrderUtils.orderByDate(files);
    }


    @Override
    protected void onPostExecute(Object o) {
        //通知继续录像或删除失败

        super.onPostExecute(o);
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        for (String file : mFileList) {
            boolean result = FileUtils.deleteFile(AW360SharePreferenceUtil.getVideoPath()+file);
            if (result) {
                if (SDCardUtils.getFreeBytes(AW360SharePreferenceUtil.getVideoPath()) > APPConfig.SMALL_FREE_SIZE) {
                    break;
                }
            }
        }
        return null;
    }
}
